/**
 * Created by yuweimei on 2018/4/19.
 */
app.controller('mypraise', function($scope, $http) {

	//显示取消点赞操作页面
	$scope.allShow1 = function(tourId) {
		$scope.tourId = tourId;
		$('.all').css("display", "block");
	};
	//隐藏删除操作页面
	$scope.allHide1 = function() {
		$('.all').css("display", "none");
	};

	$('.alert_top').click(function() {
		$('.all').css("display", "none");
	});

	//证书
	var token = localStorage.getItem('token');
	$scope.mypraiseList = [];
	$scope.mypraiseInit = function() {
		//判断有无证书
		if(!token) {
			$.toast("请先登录");
			location.href = '#login';
			return;
		}
		$scope.showCollectionList(token);
		console.log(' $scope.tourIdList:' + $scope.tourIdList);
		$http({
			method: 'get',
			url: app.netPath + '/dynamic/details/show/likes',
			headers: {
				'Authorization': token
			}
		}).then(function(res) {
			if(!res.data.error) {
				for(var i = 0; i < res.data.data.length; i++) {
					if(res.data.data[i].tourDynamics.user.userAvatarAddress) {
						res.data.data[i].tourDynamics.user.userAvatarAddress = app.netPath + '/resources/img/' + res.data.data[i].tourDynamics.user.userAvatarAddress;
						app.buildImageUrl(res.data.data[i].tourDynamics.user, 'userAvatarAddress', function() {
							$scope.$digest();
						});
					}
					//放置动态图片
					if(res.data.data[i].tourDynamics.tourdynaImg) {
						//得到每条动态的图片数组
						var dynamicImgs = app.getDynamicImg(res.data.data[i].tourDynamics.tourdynaImg);
						for(var j = 0; j < dynamicImgs.length; j++) {
							dynamicImgs[j] = app.netPath + '/resources/taskImg/' + dynamicImgs[j];
							app.buildImageUrl(dynamicImgs, j, function() {
								$scope.$digest();
							});
						}
						res.data.data[i].tourDynamics.tourdynaImg = dynamicImgs;
					}
					for(var a = 0; a < $scope.tourIdList.length; a++) {
						if(res.data.data[i].tourDynamics.tourdynaId == $scope.tourIdList[a]) {
							res.data.data[i].tourDynamics.isColl = 1;
						} else {}
					}
					var lat = res.data.data[i].tourDynamics.positionX;
					var lng = res.data.data[i].tourDynamics.positionY;
					res.data.data[i].tourDynamics.distance = app.getGreatCircleDistance(lat, lng);
					if(res.data.data[i].tourDynamics.distance >= 1000) {
						res.data.data[i].tourDynamics.distance = (app.getGreatCircleDistance(lat, lng) / 1000).toFixed(2) + "千米";
					} else {
						res.data.data[i].tourDynamics.distance = app.getGreatCircleDistance(lat, lng) + "米";
					}
					//将动态逐条存入（dynamicsList）数组
					$scope.mypraiseList.push(res.data.data[i]);
					console.log('mypraiseList:' + $scope.mypraiseList[i].tourDynamics.isColl);
				}

			} else {
				$.toast(res.data.error + ',' + res.data.msg);
			}
		}, function() {
			$.toast('网络走丢了！')
		});
	};
	//取消点赞
	$scope.reduceLike = function() {
		if(!token) {
			//判断有无证书
			$.toast("请先登录");
			location.href = '#login';
			return;
		} else {
			$.confirm('确定取消？', '', function() {
				//欲删除的动态id
				var tourId = $scope.tourId;
				$http({
					method: 'get',
					url: app.netPath + '/dynamic/details/likeReduce',
					headers: {
						'Authorization': token
					},
					params: {
						tourdynaId: tourId
					}
				}).then(function(res) {
					if(!res.data.error) {
						$('.all').css("display", "none");
						//刷新点赞列表
						$scope.mypraiseList = [];
						$.toast('已取消点赞');
						$scope.mypraiseInit();

					} else {
						$.toast(res.data.error + ',' + res.data.msg);
					}
				}, function() {
					$.toast('网络走丢了！')
				});
			}, function() {
				$('.all').css("display", "none");
				$.toast('已取消');
			});
		}
	};
	//点击收藏
	$scope.addCollection = function(tourdynaId) {
		$http({
			method: 'get',
			url: app.netPath + '/collection/add',
			headers: {
				'Authorization': token
			},
			params: {
				tourdynaId: tourdynaId
			}
		}).then(function(res) {
			if(!res.data.error) {
				$.toast('收藏成功');
				$scope.mypraiseList = [];
				//刷新页面
				$scope.mypraiseInit();
			} else {
				if(104 == res.data.error) {
					$('.all').css("display", "none");
					$.toast("已收藏，请前往我的收藏查看");
				} else {
					$.toast(res.data.error + ',' + res.data.msg);
				}
			}
		}, function() {
			$.toast('网络走丢了！')
		});
	};
	$scope.GoToReview = function(tourdynaId) {
		sessionStorage.setItem('tourdynaInfoId', tourdynaId);
	};
});